package com.study.talk.util;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.study.talk.R;
import com.study.talk.entity.GroupMessageRecord;
import com.study.talk.entity.MessageRecord;
import com.study.talk.net.OkHttp;

public class PopWindowUtil {
    private View view;
    private PopupWindow mPopupWindow;
    private int layout_id;
    private Activity mContext;
    private boolean isShowAsView;//是否在某个View下面显示
    private boolean isMatchParent;//宽高是否是Match_Parent
    private static ShapeableImageView popWindowImageView;
    private static TextView popWindowTextView;

    public PopWindowUtil(int layout_id, Activity mContext, boolean isShowAsView, boolean isMatchParent) {
        view = LayoutInflater.from(mContext).inflate(layout_id, null);
        setView(view);
        this.mContext = mContext;
        this.isShowAsView = isShowAsView;
        this.isMatchParent = isMatchParent;
        mPopupWindow = new PopupWindow(mContext);
        setPopupWindow(mPopupWindow);
    }

    public void showPop(View dropDownView) {
        // 设置布局文件
        mPopupWindow.setContentView(view);
        // 为了避免部分机型不显示，我们需要重新设置一下宽高
        if (!isMatchParent) {
            mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        }
        // 设置pop透明效果
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000));
        // 设置pop出入动画
        if (isShowAsView) {
        //mPopupWindow.setAnimationStyle(R.style.pop_add);
        }
        // 设置pop获取焦点，如果为false点击返回按钮会退出当前Activity，如果pop中有Editor的话，focusable必须要为true
        mPopupWindow.setFocusable(true);

        // 设置点击pop外侧消失，默认为false；在focusable为true时点击外侧始终消失
        // 设置pop可点击，为false点击事件无效，默认为true
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        // 相对于 + 号正下面，同时可以设置偏移量
        if (isShowAsView) {
            mPopupWindow.showAsDropDown(dropDownView, -100, -12);
        } else {
            mPopupWindow.showAtLocation(dropDownView, Gravity.TOP, 0, 0);
        }
    }


    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public PopupWindow getmPopupWindow() {
        return mPopupWindow;
    }

    public void setPopupWindow(PopupWindow mPopupWindow) {
        this.mPopupWindow = mPopupWindow;
    }


    public static void showPopWindow(Activity context, View view, MessageRecord message, BackCall call) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_layout, null);
        initView(contentView);
        popWindowTextView.setText(message.getMessageType() == 0 ? message.getContent() : "[ 图片 ]");
        Glide.with(context).load(OkHttp.URL + message.getAvatar()).into(popWindowImageView);
        PopupWindow popupWindow = new PopupWindow(context);
        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.popStyle);
        popupWindow.setTouchable(true);
        popupWindow.setContentView(contentView);
        popupWindow.showAtLocation(view, Gravity.TOP, 0, 0);
        contentView.setOnClickListener(vv -> {
            call.then();
            popupWindow.dismiss();
        });
        new Handler().postDelayed(()->{
            if (popupWindow.isShowing()){
                popupWindow.dismiss();
            }
        },5000);
    }

    public static void showPopWindow(Activity context, View view, GroupMessageRecord message, BackCall call) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_layout, null);
        initView(contentView);
        popWindowTextView.setText(message.getMessageType() == 0 ? message.getContent() : "[ 图片 ]");
        Glide.with(context).load(OkHttp.URL + message.getAvatar()).into(popWindowImageView);
        PopupWindow popupWindow = new PopupWindow(context);
        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.popStyle);
        popupWindow.setTouchable(true);
        popupWindow.setContentView(contentView);
        popupWindow.showAtLocation(view, Gravity.TOP, 0, 0);
        contentView.setOnClickListener(vv -> {
            call.then();
            popupWindow.dismiss();
        });
        new Handler().postDelayed(()->{
            if (popupWindow.isShowing()){
                popupWindow.dismiss();
            }
        },5000);
    }

    private static void initView(View view) {
        popWindowImageView = (ShapeableImageView) view.findViewById(R.id.pop_window_image_view);
        popWindowTextView = (TextView) view.findViewById(R.id.pop_window_text_view);
    }
}